/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFTimer
  \brief To represent any type of timing detectors
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2022-04-26
  \date 2022-04-26 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2022-04-26   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <cstring>
#include <TTree.h>
#include <TClonesArray.h>
#include "ETFRun.h"
#include "ETFTimer.h"
#include "ETFPlaHit.h"

ETFTimer::ETFTimer(const char *name, double length) : ETFDetector(name),
fLength(length){
  fHits = new TClonesArray("ETFPlaHit");
  fHits->SetName(name);
} // end ctor

ETFTimer::~ETFTimer(){
  if(fHits) delete fHits;
} // end dtor

void ETFTimer::Register(){
  if(fHits) fTree->Branch(fHits->GetName(), &fHits);
} // end member function Register

// initialize the detector
void ETFTimer::Initialize(){
  ETFDetector::Initialize();
  fHits->Clear();
} // end member function Initialize

int ETFTimer::GetNofHits() const{ return fHits->GetEntriesFast(); }

// return fHits->At(i);
ETFPlaHit *ETFTimer::GetHit(int i) const{
  if(i < 0 || i >= fHits->GetEntriesFast()) return nullptr;
  return static_cast<ETFPlaHit *>(fHits->At(i));
} // end member function GetHit

void ETFTimer::Print(){
  // if(!fIsFired) return;
  ETFDetector::Print();
  for(auto p : *fHits) static_cast<ETFPlaHit *>(p)->Print();
} // end member function Print

// finish the processing of all the assigned data
void ETFTimer::PostAssign(){
  for(TObject *b : *fHits){
    ETFPlaHit *h = static_cast<ETFPlaHit *>(b);
    // h->Filter(fTrigLB, fTrigHB);
    h->Assign();
  } // end if
} // end member function PostAssign
